package com.example.demo.thread.线程组和优先级;

import java.util.concurrent.TimeUnit;

/**
 * 守护线程测试类
 * 线程分为2种  用户线程和守护线程
 * 守护线程并非程序必须的,当所有用户线程结束后，程序就终止了，同时会杀死所有守护线程，不管此时守护线程在执行什么操作。
 *
 * @author ldl
 */
public class Demo5 {
    public static void main(String[] args) {

        Thread thread1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "thread1 running .....");
        }, "thread1");


        Thread daemonThread = new Thread(() -> {
            while (true) {
                try {
                    System.out.println(Thread.currentThread().getName() + "daemonThread running .....");
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "daemonThread");

        thread1.start();
        daemonThread.setDaemon(true);
        daemonThread.start();
    }
}
